const wrapAsync = require('./errorHandler')
require('dotenv').config();
const jwt = require('jsonwebtoken');
const checkToken = wrapAsync(async (req, res, next) => {
    if (!req.headers.authorization) {
        return res.status(401).json({ code: 401, message: '未授权' });
    }
    const token = req.headers.authorization.split(' ')[1];
    const decoded = jwt.verify(token, process.env.SECRET_KEY);
    const userId = decoded.id;
    req.userId = userId;
    next();
})
module.exports = checkToken;